# coding:utf-8

import uuid
import json
import logging

from constants import SESSION_EXPIRES_SECONDS


class Session(object):
    """"""
    def __init__(self, request_handler_obj):

        # 先判断用户是否已经有了session_id
        self._request_handler = request_handler_obj
        self.session_id = request_handler_obj.get_secure_cookie("session_id")
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD

=======
# request_handler_ogj   只用  cookie 和redis   不用继承  构造的时候 self 传
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
# request_handler_ogj   只用  cookie 和redis   不用继承  构造的时候 self 传
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
# request_handler_ogj   只用  cookie 和redis   不用继承  构造的时候 self 传
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
        # 如果不存在session_id,生成session_id
        if not self.session_id:
            self.session_id = uuid.uuid4().hex
            self.data = {}
            request_handler_obj.set_secure_cookie("session_id", self.session_id)

        # 如果存在session_id, 去redis中取出data
        else:
            try:
                json_data = request_handler_obj.redis.get("sess_%s" % self.session_id)
            except Exception as e:
                logging.error(e)
                raise e
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======

>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======

>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======

>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
            if not json_data:
                self.data = {}
            else:
                self.data = json.loads(json_data)
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
# 第一次访问为空
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
# 第一次访问为空
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
# 第一次访问为空
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd

    def save(self):
        json_data = json.dumps(self.data)
        try:
            self._request_handler.redis.setex("sess_%s" % self.session_id,
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
                                             SESSION_EXPIRES_SECONDS, json_data)
        except Exception as e:
            logging.error(e)
            raise e
=======
=======
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
                                              SESSION_EXPIRES_SECONDS, json_data)
            # json 的用户数据  和session_id 保持在redis
        except Exception as e:
            logging.error(e)
            raise e
            raise Exception("save session failed")
        else:
            self._request_handler.set_secure_cookie("session_id",self.session_id)
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd
=======
>>>>>>> c63c9da4a8911e39dc30b24ae91224458c4b73cd

    def clear(self):
        try:
            self._request_handler.redis.delete("sess_%s" % self.session_id)
        except Exception as e:
            logging.error(e)
        self._request_handler.clear_cookie("session_id")


"""
class xxxxhandler(RequestHandler):
    def post(self):

        session = Session(self)
        session.session_id
        session.data["username"] = "abc"
        session.data["mobile"] = "abc"
        session.save()

    def get(self):
        session = Session(self)
        session.data["username"] = "def"
        session.save()



    def get(self):
        session = Session(self)
        session.clear()

        session.clear()

redis中的数据：
key:    session_id
value:  data
"""
